package top.milkbox.core;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC工具类
 *
 * @author milkbox
 */
public class UtilsJDBC {
    // 当前的类名
    private static final String CLASSNAME = UtilsJDBC.class.getSimpleName();

    private static String driver; // 驱动路径
    private static String url; // 连接地址
    private static String user; // 用户名
    private static String password; // 密码


    // 静态代码快，随着类的加载而执行，且只执行一次
    static {
        try {
            // 读取配置文件
            // 通过类加载器获取文件的绝对路径
            ClassLoader classLoader = UtilsJDBC.class.getClassLoader();
            URL resource = classLoader.getResource("jdbc.properties");
            String path = URLDecoder.decode(resource.getPath(), "utf-8"); // 获取jdbc.properties的绝对路径
            log("配置文件所在的路径：" + path);
            // 创建配置文件对象
            Properties properties = new Properties();
            properties.load(new FileReader(path));
            // 获取数据
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            log("加载的信息：");
            log("driver：" + driver);
            log("url：" + url);
            log("user：" + user);
            log("password：" + password);

            // 注册驱动（mysql8可以省略）
            Class.forName(driver);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取一个连接
     *
     * @return 返回一个数据库连接
     * @throws SQLException 登录失败异常
     */
    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(url, user, password);
        log("已获取连接：" + connection.toString());
        return connection;
    }

    /**
     * 释放资源
     *
     * @param resultSet  结果集
     * @param statement  SQL执行
     * @param connection 连接
     */
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        log("释放资源...");
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 释放资源
     *
     * @param statement  SQL执行
     * @param connection 连接
     */
    public static void close(Statement statement, Connection connection) {
        log("释放资源...");
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    /**
     * 通过此类打印的日志
     *
     * @param string 要打印的日志
     */
    private static void log(String string) {
        System.out.println("log from " + CLASSNAME + ": " + string);
    }
}
